from django.db import models

# Create your models here.
from rest_framework import serializers

from code_monkey.libs.base_model import BaseModel
from users.models import User


class TagModel(models.Model):
    name = models.CharField(verbose_name="标签名", max_length=10)
    info = models.TextField(verbose_name="标签描述", null=True)
    quote_total = models.IntegerField(verbose_name="标签引用数")

    class Meta:
        db_table = 'tb_tag'
        verbose_name = '标签分类'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class QuestionModel(BaseModel):
    title = models.CharField(max_length=100, verbose_name="问题标题")
    author = models.ForeignKey(User, on_delete=models.CASCADE,verbose_name="问题发布人")
    count = models.IntegerField(verbose_name="问题有用数", default=0)
    detail = models.TextField(null=True, verbose_name="内容")
    comment_num = models.IntegerField(verbose_name="回答数", default=0)
    click = models.IntegerField(verbose_name="浏览数", default=0)
    tag = models.ManyToManyField(TagModel, through="QuestionTagModel")
    # tags=models.ManyToManyField(to=TagModel,through="QuestionTagModel",through_fields=("question","tag"))

    class Meta:
        db_table = 'tb_question'
        verbose_name = '问题详情'
        verbose_name_plural = verbose_name



class QuestionTagModel(models.Model):
    question = models.ForeignKey(QuestionModel, on_delete=models.CASCADE,verbose_name="问题ID")
    tag = models.ForeignKey(TagModel, models.CASCADE,verbose_name="标签ID")

    class Meta:
        db_table = 'tb_question_tag'
        verbose_name = '问题对应的标签'
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s对应的标签%s" % (self.question.title, self.tag.name)


class AnswerModel(BaseModel):
    author = models.ForeignKey(User, on_delete=models.CASCADE,verbose_name="回答发布人")
    count = models.IntegerField(verbose_name="回答有用数", default=0)
    detail = models.TextField(verbose_name="回答详情")
    question = models.ForeignKey(QuestionModel, on_delete=models.CASCADE,verbose_name="回答对应的问题", related_name="answers")
    sub_comment_num = models.IntegerField(verbose_name="该回答的子回答数", default=0)

    class Meta:
        db_table = 'tb_answer'
        verbose_name = '回答详情'
        verbose_name_plural = verbose_name

    def __str__(self):
        return "答复%s问题" % self.question


class SubAnswerModel(BaseModel):
    author = models.ForeignKey(User, on_delete=models.CASCADE,verbose_name="回答发布人")
    detail = models.TextField(verbose_name="回答详情")
    comment = models.ForeignKey(AnswerModel, on_delete=models.CASCADE,verbose_name="评论对应的回答", related_name="subs")
    # pid = models.ForeignKey("self", verbose_name="回复的评论"
    pid = models.ForeignKey(to='self', on_delete=models.CASCADE,null=True, verbose_name='父类别')

    class Meta:
        db_table = 'tb_subanswer'
        verbose_name = '子回答详情'
        verbose_name_plural = verbose_name

    def __str__(self):
        return "评论%s" % self.pid


class UserFocusTagModel(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE,verbose_name="用户ID")
    tag = models.ForeignKey(TagModel,on_delete=models.CASCADE, verbose_name="标签ID")
    is_focus = models.NullBooleanField(verbose_name='关注的状态', default=0)

    class Meta:
        db_table = 'tb_user_foucus_tag'
        verbose_name = '用户关注的标签'
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s关注了%s该标签" % (self.user.username, self.tag.name)


class QuestionFocusModel(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE,verbose_name="用户ID")
    question = models.ForeignKey(QuestionModel, on_delete=models.CASCADE,verbose_name="标签ID")
    is_click = models.BooleanField(default=True, verbose_name="关注")

    class Meta:
        db_table = 'tb_user_foucus_question'
        verbose_name = '用户收藏的问题'
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s对应%s标签" % (self.user.username, self.question.title)


class UsefulChangeModel(models.Model):
    GENDER_CHOICES = (
        (1, '没有用'),
        (2, '不评价'),
        (3, '有用'),
    )
    user = models.ForeignKey(User, on_delete=models.CASCADE,verbose_name="用户ID")
    question = models.ForeignKey(QuestionModel,on_delete=models.CASCADE, verbose_name="标签ID")
    # is_useful = models.IntegerField(default=0, verbose_name="是否有用")
    is_useful = models.SmallIntegerField(choices=GENDER_CHOICES, default=2, verbose_name='是否有帮助')

    class Meta:
        db_table = 'tb_user_useful_question'
        verbose_name = '用户赞踩的问题'
        verbose_name_plural = verbose_name

class AnswerUsefulModel(models.Model):
    GENDER_CHOICES = (
        (1, '没有用'),
        (2, '不评价'),
        (3, '有用'),
    )
    user = models.ForeignKey(User,on_delete=models.CASCADE, verbose_name="用户ID")
    answer = models.ForeignKey(AnswerModel, on_delete=models.CASCADE,verbose_name="标签ID")
    # is_useful = models.IntegerField(default=0, verbose_name="是否有用")
    is_useful = models.SmallIntegerField(choices=GENDER_CHOICES, default=2, verbose_name='是否有帮助')

    class Meta:
        db_table = 'tb_user_useful_answer'
        verbose_name = '用户赞踩的回答'
        verbose_name_plural = verbose_name







